package com.demo.travel.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.demo.travel.R;
import com.demo.travel.base.BaseActivity;
import com.demo.travel.bean.UserBean;
import com.demo.travel.db.UserDao;
import com.demo.travel.utils.Config;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {


    @BindView(R.id.usernameEt)
    EditText usernameEt;

    @BindView(R.id.passwordEt)
    EditText passwordEt;

    @BindView(R.id.backIv)
    ImageView backIv;

    private UserDao userDao;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        backIv.setVisibility(View.VISIBLE);
        userDao = new UserDao(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public String getActivityTitle() {
        return "登录";
    }

    @OnClick({R.id.regTv,R.id.loginTv,R.id.backIv})
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.regTv:
                goOtherActivity(RegisterActivity.class);
                break;
            case R.id.loginTv:
                login();
                break;
            case R.id.backIv:
                finish();
                break;
        }
    }

    private void login() {
        if (TextUtils.isEmpty(usernameEt.getText()) ||
                TextUtils.isEmpty(passwordEt.getText())
        ) {
            showToast("输入内容不能为空！");
            return;
        }
        UserBean userBean = userDao.checkLogin(usernameEt.getText().toString(), passwordEt.getText().toString());
        if ( userBean!= null) {
            Config.userBean = userBean;
            goOtherActivity(MainActivity.class);
        }else{
            showToast("用户名或密码错误！");
        }
    }


}
